//
//  BKGLTVTitleView.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/24.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "BKGLTVTitleView.h"

@implementation BKGLTVTitleView

- (instancetype)initWithFrame:(CGRect)frame andArray:(NSArray *)array{
    if ([super initWithFrame:frame]) {
        _dataArray = array;
        [self initSubView];
    }
    return self;
}
#pragma mark - 6/6修改了
- (void)initSubView{
    UILabel * contLab = [UILabel new];
    CGFloat W = 1.0/_dataArray.count;
    for (int i = 0; i<_dataArray.count; i++){
        UILabel *label = [[UILabel alloc] init];
        label.frame = CGRectMake(W*i,0,W,44);
        label.numberOfLines = 0;
        label.textAlignment = 1;
        label.text = _dataArray[i];
        label.tag = 30+i;
        label.font = [UIFont systemFontOfSize:14];
        label.textColor = [UIColor darkGrayColor];
        [self addSubview:label];
        if (i == 0) {
            contLab = label;
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(@0);
                make.centerY.equalTo(self.mas_centerY);
                make.width.equalTo(self).multipliedBy(W);
                make.height.mas_equalTo(40);
            }];
        } else if (i==_dataArray.count-1){
            self.selectedAllButton.frame = CGRectMake(W*i,0,W,44);
            [self addSubview:_selectedAllButton];
            [_selectedAllButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(contLab.mas_right);
                make.centerY.equalTo(self.mas_centerY);
                make.width.equalTo(self).multipliedBy(W);
                make.height.mas_equalTo(40);
            }];
        }else {
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(contLab.mas_right);
                make.centerY.equalTo(self.mas_centerY);
                make.width.equalTo(self).multipliedBy(W);
                make.height.mas_equalTo(40);
            }];
            contLab = label;
        }
    }
//    CGFloat W = self.frame.size.width/_dataArray.count;
//    for (int i = 0; i<_dataArray.count; i++){
//        if (i==_dataArray.count-1) {
//            self.selectedAllButton.frame = CGRectMake(W*i,0,W,44);
//            [self addSubview:_selectedAllButton];
//        }else{
//            UILabel *label = [[UILabel alloc] init];
//            label.frame = CGRectMake(W*i,0,W,44);
//            label.numberOfLines = 0;
//            label.textAlignment = 1;
//            label.text = _dataArray[i];
//            label.tag = 30+i;
//            label.font = [UIFont systemFontOfSize:14];
//            label.textColor = [UIColor darkGrayColor];
//            [self addSubview:label];
//        }
//
//    }
}
- (UIButton *)selectedAllButton{
    if (!_selectedAllButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@" 选择" forState:UIControlStateNormal];
        [button setTitleColor:TITLECOLOR forState:UIControlStateNormal];
//        [button setImage:[UIImage imageNamed:@"fudao_normol"] forState:UIControlStateNormal];
//        [button setImage:[UIImage imageNamed:@"fudao_selected"] forState:UIControlStateSelected];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
//        [button addTarget:self action:@selector(selectedALL:) forControlEvents:UIControlEventTouchUpInside];
        _selectedAllButton = button;
    }
    return _selectedAllButton;
}
- (void)selectedALL:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected == YES) {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"BKGLTVTitleViewSelectedAllNotice" object:nil userInfo:@{@"status":@"1"}];
    }else{
        [[NSNotificationCenter defaultCenter]postNotificationName:@"BKGLTVTitleViewSelectedAllNotice" object:nil userInfo:@{@"status":@"0"}];
    }
}
- (void)drawRect:(CGRect)rect{
    
}
- (void)reloadView{
//    CGFloat W = self.frame.size.width/_dataArray.count;
//    for (int i = 0; i<_dataArray.count; i++){
//        if (i==_dataArray.count-1) {
//            self.selectedAllButton.frame = CGRectMake(W*i,0,W,44);
//        }else{
//            UILabel *label = (UILabel *)[self viewWithTag:30+i];
//            label.frame = CGRectMake(W*i,0,W,44);
//            label.text = _dataArray[i];
//        }
//
//    }
}
@end
